﻿using System;
using System.ComponentModel;
using uNhAddIns.Entities;
using uNhAddIns.Adapters;
using Microsoft.Practices.ServiceLocation;
using System.Collections.Generic;
using System.Linq;

namespace MM.Domain
{
    public class BaseEntity : Entity, IDataErrorInfo
    {
        private static readonly IEntityValidator entityValidator;

        public static IEntityValidator EntityValidator {

            get {
                if (entityValidator == null) {
                    return ServiceLocator
                        .Current
                        .GetInstance<IEntityValidator>();
                }

                return entityValidator;
            }
        }

        #region Miembros de IDataErrorInfo

        string IDataErrorInfo.this[string columnName] {

            get {
                var errors = EntityValidator
                            .Validate(this, columnName)
                            .Select(iv => iv.Message)
                            .ToArray();

                return string.Join(Environment.NewLine, errors);
            }
        }

        string IDataErrorInfo.Error {
            get {
                var errors = EntityValidator
                            .Validate(this)
                            .Select(iv => iv.Message)
                            .ToArray();

                return string.Join(Environment.NewLine, errors);
            }
        }

        #endregion
    }
}
